package cn.demo.common.core.domain.entity;

import cn.demo.common.annotation.Excel;
import cn.demo.common.annotation.Excel.ColumnType;
import cn.demo.common.annotation.Excel.Type;
import cn.demo.common.annotation.Excels;
import cn.demo.common.core.domain.BaseEntity;
import cn.demo.common.core.domain.model.UserInfo;
import cn.demo.common.mybatisFlex.MyInsertListener;
import cn.demo.common.mybatisFlex.MyUpdateListener;
import cn.demo.common.utils.bean.BeanUtils;
import cn.demo.common.xss.Xss;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mybatisflex.annotation.*;
import com.mybatisflex.core.constant.SqlOperator;
import com.mybatisflex.core.query.SqlOperators;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Date;
import java.util.List;

import static cn.demo.common.constant.CommonConstants.ADMIN_ID;

/**
 * 用户对象 sys_user
 *
 * @author ruoyi
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Table(value = "sys_user",onInsert = MyInsertListener.class,onUpdate = MyUpdateListener.class)
public class SysUser extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * 用户ID
     */
    @Id
    @Excel(name = "用户序号", cellType = ColumnType.NUMERIC, prompt = "用户编号")
    private String userId;

    /**
     * 部门ID
     */
    @Excel(name = "部门编号", type = Type.IMPORT)
    private String deptId;

    /**
     * 登录账号
     */
    @Excel(name = "登录账号")
    @Xss(message = "登录账号不能包含脚本字符")
    @NotBlank(message = "登录账号不能为空")
    @Size(min = 0, max = 30, message = "登录账号长度不能超过30个字符")
    private String userName;

    /**
     * 用户昵称
     */
    @Excel(name = "用户昵称")
    @Xss(message = "用户昵称不能包含脚本字符")
    @Size(min = 0, max = 30, message = "用户昵称长度不能超过30个字符")
    private String nickName;

    /**
     * 用户邮箱
     */
    @Excel(name = "用户邮箱")
    @Email(message = "邮箱格式不正确")
    @Size(min = 0, max = 50, message = "邮箱长度不能超过50个字符")
    private String email;

    /**
     * 手机号码
     */
    @Excel(name = "手机号码", cellType = ColumnType.TEXT)
    @Size(min = 0, max = 11, message = "手机号码长度不能超过11个字符")
    private String phoneNumber;

    /**
     * 用户性别
     */
    @Excel(name = "用户性别", readConverterExp = "0=男,1=女,2=未知")
    private Integer sex;

    /**
     * 用户头像
     */
    private String avatar;

    /**
     * 密码
     */
    private String password;

    /**
     * 帐号状态（0正常 1停用）
     */
    @Excel(name = "帐号状态", readConverterExp = "0=正常,1=停用")
    private Integer status;

    /**
     * 删除标志（0代表存在 1代表删除）
     */
    private Integer delFlag;

    /**
     * 最后登录IP
     */
    @Excel(name = "最后登录IP", type = Type.EXPORT)
    private String loginIp;

    /**
     * 最后登录时间
     */
    @Excel(name = "最后登录时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss", type = Type.EXPORT)
    private Date loginDate;

    /**
     * 部门对象
     */
    @Excels({
            @Excel(name = "部门名称", targetAttr = "deptName", type = Type.EXPORT),
            @Excel(name = "部门负责人", targetAttr = "leader", type = Type.EXPORT)
    })
    @RelationOneToOne(selfField = "deptId", targetField = "deptId")
    private SysDept dept;

    /**
     * 角色对象
     */
    @RelationManyToMany(joinTable = "sys_user_role",joinSelfColumn = "user_id",joinTargetColumn = "role_id")
    private List<SysRole> roles;

    /**
     * 角色组
     */
    private String[] roleIds;

    /**
     * 岗位组
     */
    private String[] postIds;

    /**
     * 角色ID
     */
    @Column(ignore = true)
    private String roleId;

    // 自定义字段的 SQL 操作符
    @JsonIgnore
    public static final SqlOperators operators = SqlOperators.of()
            .set(SysUser::getUserName, SqlOperator.LIKE)
            .set(SysUser::getNickName, SqlOperator.LIKE)
            .set(SysUser::getPhoneNumber, SqlOperator.LIKE);

    public SysUser() {

    }

    public SysUser(String userId) {
        this.userId = userId;
    }

    public boolean isAdmin() {
        return isAdmin(this.userId);
    }

    public static boolean isAdmin(String userId) {
        return ADMIN_ID.equals(userId);
    }

    public UserInfo buildUserInfo() {
        return BeanUtils.copy(this, UserInfo.class);
    }
}
